<?php
//验证工具类
class ValidateUtil {
 	/** 验证邮箱格式  **/	
	public static function isEmail($str){
		$chars = "/^([a-z0-9+_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,6}\$/i";
		if (preg_match($chars, $str)){
			return true;
		}else{
			return false;
		}
	}		
	/** 全字母格式  **/
	public static function isChar($str,$minlen=0,$maxlen=0,$isempty=false){
		$str = isset($str)?trim($str):'';
		if($str == ''){
			return $isempty?1:0;
		}else{
			if($maxlen != 0){
				$chars = "/^[a-zA-Z]{".$minlen.",".$maxlen."}\$/i";
				return preg_match($chars, $str)?1:2;
			}else{
				$chars = "/^[a-zA-Z]"+($minlen>0?('{'.$minlen.',}'):'+')+"\$/i";
				return preg_match($chars, $str)?1:2;
			}
		}
	}
	/** 字母或数字格式  **/
	public static function isCharNum($str,$minlen=0,$maxlen=0,$isempty=false){
		$str = isset($str)?trim($str):'';
		if($str == ''){
			return $isempty?1:0;
		}else{
			if($maxlen != 0){
				$chars = "/^[a-zA-Z0-9]{".$minlen.",".$maxlen."}\$/i";
				return preg_match($chars, $str)?1:2;
			}else{
				$chars = "/^[a-zA-Z0-9]"+($minlen>0?('{'.$minlen.',}'):'+')+"\$/i";
				return preg_match($chars, $str)?1:2;
			}
		}
	}
	/** 全数字格式  **/
	public static function isNum($str,$minlen=0,$maxlen=0,$isempty=false){
		$str = isset($str)?trim($str):'';
		if($str == ''){
			return $isempty?1:0;
		}else{
			if($maxlen != 0){
				$chars = "/^[0-9]{".$minlen.",".$maxlen."}\$/i";
				return preg_match($chars, $str)?1:2;
			}else{
				$chars = "/^[0-9]"+($minlen>0?('{'.$minlen.',}'):'+')+"\$/i";
				return preg_match($chars, $str)?1:2;
			}
		}
	}
	/** 全汉字格式  **/
	public static function isChinese($str,$minlen=0,$maxlen=0,$isempty=false){
		$str = isset($str)?trim($str):'';
		if($str == ''){
			return $isempty?1:0;
		}else{
			if($maxlen != 0){
				$chars = "/^[\x{4e00}-\x{9fa5}]{".$minlen.",".$maxlen."}\$/i";
				return preg_match($chars, $str)?1:2;
			}else{
				$chars = "/^[\x{4e00}-\x{9fa5}]"+($minlen>0?('{'.$minlen.',}'):'+')+"\$/i";
				return preg_match($chars, $str)?1:2;
			}
		}
	}
	/** 字母，数字或汉字格式  **/
	public static function isCharNumChinese($str,$minlen=0,$maxlen=0,$isempty=false){
		$str = isset($str)?trim($str):'';
		if($str == ''){
			return $isempty?1:0;
		}else{
			if($maxlen != 0){
				$chars = "/^[0-9a-zA-Z\x{4e00}-\x{9fa5}]{".$minlen.",".$maxlen."}\$/i";
				return preg_match($chars, $str)?1:2;
			}else{
				$chars = "/^[0-9a-zA-Z\x{4e00}-\x{9fa5}]"+($minlen>0?('{'.$minlen.',}'):'+')+"\$/i";
				return preg_match($chars, $str)?1:2;
			}
		}
	}
	/** 手机格式  **/
	public static function isPhone($str){
		$str = isset($str)?trim($str):'';
		$chars = "/^(13|15|18|14)\d{9}\$/i";
		return preg_match($chars, $str)?1:0;
	}
	/** 用户名格式 (数字 字母 下划线) **/
	public static function isName($str,$minlen=4,$maxlen=20){
		$str = isset($str)?trim($str):'';
		if($str == ''){ return 0; }
		if(preg_match("/^[0-9]+\$/i", $str)){return 2;}//全数字
		if(count($str)<$minlen || count($str) > $maxlen){return 3;}//长度不符
		$chars = "/^[a-zA-Z0-9_]{".$minlen.",".$maxlen."}\$/i";
		return preg_match($chars, $str)?1:4;
	}
	
	public static function checkUploadImg($fieldname,$maxsize=200000){
		//验证图片
		if(is_uploaded_file($_FILES[$fieldname]['tmp_name'])){
			if($_FILES[$fieldname]['size'] > 0){
				if($_FILES[$fieldname]['size'] > $maxsize){
					return 3;//图片过大
				}else{
					if($_FILES[$fieldname]['type'] == "image/gif" ||
							$_FILES[$fieldname]['type'] == "image/jpeg" ||
							$_FILES[$fieldname]['type'] == "image/x-png" ||
							$_FILES[$fieldname]['type'] == "image/jpg" ||
							$_FILES[$fieldname]['type'] == "image/pjpeg" ||
							$_FILES[$fieldname]['type'] == "image/png"){
						return 1;//
					}else{
						return 4;//非指定图片类型
					}
				}
			}else{
				return 2;//图片为空
			}
		}else{
				return 0;//非法上传
		}
	}
	
	public static function checkUploadFtl($fieldname,$maxsize=200000){
		//验证图片
		if(is_uploaded_file($_FILES[$fieldname]['tmp_name'])){
			if($_FILES[$fieldname]['size'] > 0){
				if($_FILES[$fieldname]['size'] > $maxsize){
					return 3;//文件过大
				}else{
					$filename = $_FILES[$fieldname]['name'];
					$filename = substr($filename, strrpos($filename, ".")+1, strlen($filename)-strrpos($filename, "."));
					//$_FILES[$fieldname]['type'] == "application/octet-stream"
					if($_FILES[$fieldname]['type'] == "text/plain"){
						return 1;//
					}else{
						return 4;//非指定文件类型
					}
				}
			}else{
				return 2;//文件为空
			}
		}else{
			return 0;//非法上传
		}
	}
	
	/** 验证价格格式  **/
	public static function isPrice($str){
		$str = isset($str)?trim($str):'';
		$chars = "/^0$|^0\.\d{1,2}$|^[1-9]\d{0,9}$|^[1-9]\d{0,9}\.\d{1,2}?$/";
		return preg_match($chars, $str)?1:0;
	}
	
	
}
?>